"""
分类序列化器 - 门户网站
"""

from rest_framework import serializers
from pecco_site.models import Category


class CategorySerializer(serializers.Serializer):
    """分类序列化器"""
    id = serializers.IntegerField()
    slug = serializers.CharField()
    name = serializers.CharField()
    icon = serializers.SerializerMethodField()
    
    def get_icon(self, obj):
        """获取分类图标"""
        if hasattr(obj, 'icon_url'):
            return obj.icon_url
        if hasattr(obj, 'icon') and obj.icon:
            request = self.context.get('request')
            if request:
                return request.build_absolute_uri(obj.icon.url)
            return obj.icon.url
        return None

